<template>
  <div class="app-container">
  <div class="filter-container">
        
        <el-input clearable v-model="search.number" placeholder="工单号" style="width: 170px;margin-left:0px;"></el-input>
        <el-input clearable v-model="search.number" placeholder="车牌号" style="width: 170px;margin-left:10px;"></el-input>
        
        <el-date-picker v-model="search.daterange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="margin-left:10px;">
        </el-date-picker>

        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe  style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>
      
      <el-table-column  min-width="100" align="center" label="工单号">
        <template slot-scope="scope">
          <span>{{ scope.row.partcode }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="车牌号">
        <template slot-scope="scope">
          <span>{{ scope.row.partcode }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="具体车型">
        <template slot-scope="scope">
          <span>{{ scope.row.partname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="联系人">
        <template slot-scope="scope">
          <span>{{ scope.row.partname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="电话">
        <template slot-scope="scope">
          <span>{{ scope.row.partname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="工单类型">
        <template slot-scope="scope">
          <span>{{ scope.row.partname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="精品名称">
        <template slot-scope="scope">
          <span>{{ scope.row.partname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="加装方">
        <template slot-scope="scope">
          <span>{{ scope.row.partname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="制单人">
        <template slot-scope="scope">
          <span>{{ scope.row.guider }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state  }}</span>
        </template>
      </el-table-column>
      
      <el-table-column  min-width="100" align="center" label="日期">
        <template slot-scope="scope">
          <span>{{ scope.row.createtime | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="操作"  width="280" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="tableRowClick().confirm(scope.row.id)">确认完工</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().doing(scope.row.id)">转处理</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().record(scope.row.id)">出库记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="fetchData" />


<!-- 出库单详细 -->
<el-dialog :title="dialogDetails.title" :visible.sync="dialogDetails.visible"  width="700px">
    <div class="ci" ref="printNav">
      <div class="item">
          <span class="name">出库明细单号：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
          <span class="name">时间：</span>
          <span class="value">
              {{dialogDetails.data.createtime}}
          </span>
      </div>
      <div class="item">
          <span class="name">操作人：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
          <span class="name">领料人：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
      </div>
      <div class="item">
          <span class="name">精品名称：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
          <span class="name">编码：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
      </div>

      <div class="item">
          <span class="name">出库数量：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
          <span class="name">所需数量：</span>
          <span class="value">
              {{dialogDetails.data.number}}
          </span>
      </div>
      
      
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetails.visible = false">取 消</el-button>
        <el-button type="primary" @click="dialogDetailsClick().print()" :loading="dialogDetails.saveloading">打 印</el-button>
    </div>
</el-dialog>
<!-- end  出库单详细 -->

<!-- 转处理 -->
<el-dialog :title="dialogDoing.title" :visible.sync="dialogDoing.visible"  width="900px">
    <div class="ci">
        <div class="item">
            <span class="name">选择加装方：</span>
            <span class="value">
                <el-select v-model="dialogDoing.data.extraoption" clearable placeholder="--" style="width: 220px;">
                    <el-option 
                    v-for="(item,index) in selectOptions.extraoption"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </span>
        </div>
        <div class="item" v-show="dialogDoing.data.extraoption==1">
            <span class="name">具体班组：</span>
            <span class="value">
                <el-input clearable v-model="dialogDoing.data.group" style="width:220px;"></el-input>
            </span>
        </div>
        <div class="item">
            <span class="name">采购单价：</span>
            <span class="value">
                <el-input clearable v-model="dialogDoing.data.price" style="width:220px;"></el-input>
            </span>
        </div>
        <div class="item">
            <span class="name">采购单价：</span>
            <span class="value">
                <el-input clearable v-model="dialogDoing.data.price" style="width:220px;"></el-input>
            </span>
        </div>
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button   @click="dialogDoing.visible = false">关 闭</el-button>
        <el-button  type="primary" @click="dialogDoingClick().save()">保 存</el-button>
    </div>
</el-dialog>
<!-- end 转处理 -->


  </div>
</template>

<script>
import * as partsApi from '@/api/finance/parts'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import * as enumHelper from '@/utils/enum'
import {objectMerge,objectEmpty} from '@/utils/index'
import Print from '@/utils/print'

let printHandler;

export default {
    components: { Pagination },
    data(){
        return{
            search:{ 
                number:'',
                customer:'',
                phone:'',
                daterange:'',
            },
            listQuery: {
                page: 1,
                rows: 20,
                name:'',
                phone:'',
                state:-1,
            },
            list:null,
            listLoading:false,
            total:0,
            selectOptions:{
                extraoption:[
                    {label:'内部班组',value:'1'},
                    {label:'外部其他',value:'2'},
                ]
            },

            
            dialogDoing:{
                visible:false,
                saveloading:false,
                title:'转处理',
                data:{
                    id:'',
                    partcode:'',
                    partname:'',
                    count:'',
                    storename:'',
                    storeplace:'',
                }
            },
            dialogDetails:{
                visible:false,
                saveloading:false,
                title:'出库单查询',
                data:{
                    id:'',
                    partcode:'',
                    partname:'',
                    count:'',
                    storename:'',
                    storeplace:'',
                }
            }
        }
    },
    created(){
        var that=this;
        //初始化下拉菜单
        //enumHelper.initOptions(that.selectOptions.salesState,enumHelper.presale.salesState);
        that.fetchData();
    },
    filters:{
      parseFixType(val){
        return enumHelper.aftersale.fixType.getName(val);
      },
      parseFixCategory(val){
        return enumHelper.aftersale.fixCategory.getName(val);
      },
      parseClaimdamageState(val){
        return enumHelper.aftersale.claimdamageState.getName(val);
      }

    },
    methods:{
        fetchData(){
          var that=this;
            partsApi.getPartspurchaseList({}).then(res=>{
              if(res.data){
                that.list=[...res.data.items.map((item,index)=>{
                  item._id=index+1;
                  return item;
                })];
                that.total=res.data.total;
              }
            })
        },
        refreshClick(){
          this.fetchData();
        },

        tableRowClick(){
            var that=this;
            return {
                confirm(id){
                    that.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',  cancelButtonText: '取消', type: 'warning'
                    }).then(() => {
                        that.$message({
                            type: 'success',
                            message: '处理成功!'
                        });
                    }).catch(() => {
                        that.$message({
                            type: 'info',
                            message: '已取消'
                        });
                    });
                },
                doing(id){
                    var data=that.list.filter(o=>o.id==id)[0];
                    that.dialogDoingClick().init(data);
                    that.dialogDoing.visible=true;
                },

                record(id){
                    var data=that.list.filter(o=>o.id==id)[0];
                    that.dialogDetailsClick().init(data);
                    that.dialogDetails.visible=true;
                },
            }
        },
        
        dialogDoingClick(){
            var that=this;
            var dg=that.dialogDoing;
            return {
                init(data){
                    data?objectMerge(dg.data,data):objectEmpty(dg.data);
                },
                save(){
                    dg.visible=false;
                }
            }
        },
        dialogDetailsClick(){
            var that=this;
            var dg=that.dialogDetails;
            return {
                init(data){
                    data?objectMerge(dg.data,data):objectEmpty(dg.data);
                },
                print(){
                    if(!printHandler){
                        printHandler=new Print(that.$refs['outstorebill'])
                    }
                    printHandler.Start();
                }
            }
        },

    }
}
</script>

<style>

</style>